from strategy_mode import dataLoader, str_cal, Trading, TradingRules

# 上涨模式
def UpMode(
    df_day, df_day_1, preTYPES, mode, tradePersent, day_lists, day, statsdic, count_in
):

    return mode, tradePersent


# 下跌模式
def DownMode(
    df_day, df_day_1, preTYPES, mode, tradePersent, day_lists, day, statsdic, count_in
):

    return mode, tradePersent


def RoundMode(
    df_day, df_day_1, preTYPES, mode, tradePersent, day_lists, day, statsdic, count_in
):

    return mode, tradePersent


def Main(
    df_day,
    df_day_1,
    mode="BUY/SELL",
    line_Pares=5,
    tradePersent=1,
    day_lists=[],
    day=0,
    statsdic={},
    count_in=1,
):

    gap = 4 / len(day_lists)
    types = statsdic["TYPES"]
    tradePersent = 0

    if (
        df_day["open_sort"] < 0
        and df_day["close_sort"] < 0
        and df_day["low_sort"] < 0
        and df_day["high_sort"] < 0
    ) and (
        df_day["open_by%Boll_sum"] < 0
        and df_day["close_by%Boll_sum"] < 0
        and df_day["low_by%Boll_sum"] < 0
        and df_day["high_by%Boll_sum"] < 0
    ):
        types = "LOW"

    elif (
        df_day["open_sort"] > 0
        and df_day["close_sort"] > 0
        and df_day["low_sort"] > 0
        and df_day["high_sort"] > 0
    ) and (
        df_day["open_by%Boll_sum"] > 0
        and df_day["close_by%Boll_sum"] > 0
        and df_day["low_by%Boll_sum"] > 0
        and df_day["high_by%Boll_sum"] > 0
    ):  # 上涨模式
        types = "HIGH"

    else:  # 横盘模式
        types = "ROUND"


    if types == "LOW":
        if statsdic["Share"] > 0 and (
            (
                round(df_day["close_by%Boll_sum"], 2)
                == round(df_day["open_by%Boll_sum"], 2)
                == round(df_day["low_by%Boll_sum"], 2)
                == round(df_day["high_by%Boll_sum"], 2)
            )
        ):  # TODO 添加过滤器
            mode = "SELL"
            tradePersent = 0.5
        elif (
            (
                (
                    df_day_1["open_sort"]
                    == df_day_1["close_sort"]
                    == df_day_1["low_sort"]
                    == df_day_1["high_sort"]
                )
                and (
                    df_day_1["open_by%Boll_sum"] <= -0.7
                    and df_day_1["close_by%Boll_sum"] <= -0.7
                    and df_day_1["low_by%Boll_sum"] <= -0.7
                    and df_day_1["high_by%Boll_sum"] <= -0.7
                )
            )
            or (
                (
                    df_day_1["open_by%Boll_sum"]
                    == df_day_1["close_by%Boll_sum"]
                    == df_day_1["low_by%Boll_sum"]
                    == df_day_1["high_by%Boll_sum"]
                )
                and (
                    df_day_1["open_sort"] <= -0.7
                    and df_day_1["close_sort"] <= -0.7
                    and df_day_1["low_sort"] <= -0.7
                    and df_day_1["high_sort"] <= -0.7
                )
            )
            and (
                df_day_1["open_by%Boll_sum"] < df_day["open_by%Boll_sum"]
                or df_day_1["close_by%Boll_sum"] < df_day["close_by%Boll_sum"]
                or df_day_1["low_by%Boll_sum"] < df_day["low_by%Boll_sum"]
                or df_day_1["high_by%Boll_sum"] < df_day["high_by%Boll_sum"]
            )
            and df_day_1["close"] < df_day["close"]
            and df_day["close"] < df_day["Avg"] < df_day["high"]
        ):
            # TODO 增加下跌过滤,避免暴跌买入
            mode = "BUY"
            tradePersent = 0.3

    elif types == "HIGH":
        if statsdic["Share"] > 0 and (
            (
                (
                    round(df_day_1["All_by%Boll_sum"], 2) == 1
                    and round(df_day_1["All_sort"], 2) == 1
                )
                and (
                    round(df_day["All_by%Boll_sum"], 2)
                    < round(df_day_1["All_by%Boll_sum"], 2)
                    or round(df_day["All_sort"], 2)
                    < round(df_day_1["All_by%Boll_sum"], 2)
                )
            )
            or (
                (
                    df_day_1["close-open_ma_Main"] >= 1
                    or df_day_1["high-low_ma_Main"] >= 1
                )
                and (
                    df_day["close-open_ma_Main"] < df_day_1["close-open_ma_Main"]
                    or df_day["high-low_ma_Main"] < df_day_1["high-low_ma_Main"]
                )
            )
        ):
            mode = "SELL"
            tradePersent = 0.5
            # TODO 建立过滤器
        elif (
            (
                round(df_day["All_by%Boll_sum"], 2) <= 0.2
                and round(df_day["All_sort"], 2) <= 0.2
            )
            and (
                round(df_day_1["All_by%Boll_sum"], 2)
                <= round(df_day["All_by%Boll_sum"], 2)
                and round(df_day_1["All_sort"], 2) <= round(df_day["All_sort"], 2)
            )
            # and (
            #     df_day_1["open_by%Boll_sum"] < df_day["open_by%Boll_sum"]
            #     or df_day_1["close_by%Boll_sum"] < df_day["close_by%Boll_sum"]
            #     or df_day_1["low_by%Boll_sum"] < df_day["low_by%Boll_sum"]
            #     or df_day_1["high_by%Boll_sum"] < df_day["high_by%Boll_sum"]
            # )
            # and (
            #     # df_day_1["open_ma_13"] < df_day["open_ma_13"]  # 待验证
            #     # and df_day_1["open_ma_21"] < df_day["open_ma_21"]  # 待验证
            #     # and
            #     df_day_1["open_ma_34"] < df_day["open_ma_34"]  # 待验证
            # )
        ):
            mode = "BUY"
            tradePersent = 0.1

    elif types == "ROUND":
        # TODO 建立过滤器
        if statsdic["Share"] > 0 and (
            (
                (
                    round(df_day_1["All_by%Boll_sum"], 2) == 1
                    and round(df_day_1["All_sort"], 2) == 1
                )
                and (
                    round(df_day["All_by%Boll_sum"], 2)
                    < round(df_day_1["All_by%Boll_sum"], 2)
                    or round(df_day["All_sort"], 2)
                    < round(df_day_1["All_by%Boll_sum"], 2)
                )
            )
            and (
                (
                    df_day_1["close-open_ma_Main"] >= 1
                    or df_day_1["high-low_ma_Main"] >= 1
                )
                and (
                    df_day["close-open_ma_Main"] < df_day_1["close-open_ma_Main"]
                    or df_day["high-low_ma_Main"] < df_day_1["high-low_ma_Main"]
                )
            )
        ):
            mode = "SELL"
            tradePersent = 1
        # elif (
        #     round(df_day["All_by%Boll_sum"], 2) <= -1
        #     and round(df_day["All_sort"], 2) <= -1
        # ) and (
        #     round(df_day["close_by%Boll_sum"], 2)
        #     == round(df_day["open_by%Boll_sum"], 2)
        #     == round(df_day["low_by%Boll_sum"], 2)
        #     == round(df_day["high_by%Boll_sum"], 2)
        # ):
        #     mode = "BUY"
        #     tradePersent = 0.5
    statsdic["TYPES"] = types
    return mode, tradePersent, statsdic
